## A file to create working files for the different designs and adds the
## census data from 2006 and 2016

library(here)
library(dplyr)

load(here("Data", "big_wrkdat_thin.rda"))

### Versions using the latest release of the 2006 census data
## File with only whites, only people with not super huge maps
wrkdat_new <- big_wrkdat_thin %>%
  filter(white == 1 & !is.na(da_prop_vm_20pct_06) & !is.na(csd.pop) & !is.na(csd_urban_06) &
    community_area_km < quantile(community_area_km, .99, na.rm = TRUE)) %>%
  droplevels()

save(wrkdat_new, file = here("Data", "wrkdat_new.rda"))
## File with all races
wrkdat_all_race_new <- big_wrkdat_thin %>%
  filter(!is.na(da_prop_vm_20pct_06) & !is.na(csd.pop) & !is.na(csd_urban_06) &
    community_area_km < quantile(community_area_km, .99, na.rm = TRUE)) %>%
  droplevels()

## Make a data files with only valid data on at least one of the perceptions of visible minorities
## File with only whites
wrkdatOwnMap_new <- droplevels(subset(wrkdat_new, subset = !allperceptionsmissing))
quantile(wrkdatOwnMap_new$community_area_km, seq(.9, 1, .01), na.rm = TRUE)
save(wrkdatOwnMap_new, file = here("Data", "wrkdatOwnMap_new.rda"))

## File with all races and valid perceptions
wrkdatOwnMap_all_race_new <- droplevels(subset(wrkdat_all_race_new, subset = !allperceptionsmissing))
save(wrkdatOwnMap_all_race_new, file = here("Data", "wrkdatOwnMap_all_race_new.rda"))

## Restricted to only those observations with valid perceptions of the DA
wrkdat_DA0_new <- droplevels(subset(wrkdat_new, subset = display.district == "da" & !allperceptionsmissing))
wrkdat_DA0_new$community2 <- NULL

save(wrkdat_DA0_new, file = here("Data", "wrkdat_DA0_new.rda"))

system("touch make_working_files.done")
